/*
   100~300之间有201个整数, 如果判断一个整数是否为质数需要1s, 
   现在要求,判断201个整数是否为质数共消耗1s 
 */
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>

#define START  100
#define END    300
#define THR_NR ((END) - (START) + 1)

void *job_thread (void *arg);

int main (void)
{
	int i = 0;
	int j = 0;
	int err = 0;
	pthread_t tids[THR_NR] = {0};
	int arr[THR_NR] = {0};

	for (i = START, j = 0; i <= END; i++, j++) {
		//err = pthread_create (tids + i - START, NULL, job_thread, (void *)&i);
		arr[j] = i;
		err = pthread_create (tids + i - START, NULL, job_thread, arr + j);
		if (err) {
			fprintf (stderr, "pthread_create ():%s", strerror(err));
			exit (1);
		}
	}
	//等着给工作线程收尸
	for (i = 0; i < THR_NR; i++){
		pthread_join (tids[i], NULL);
	}
	return 0;
}

static int is_primer (int n) {
	int i = 0;
	if (n < 2) {
		return 0;
	}
	for (i = 2; i < n; i++) {
		if (n % i == 0) {
			return 0;
		}
	}
	return 1;
}

void *job_thread (void *arg) {
	int n = *((int *)arg);
	if (is_primer (n)) {
		printf ("%d is a primer!\n", n);
	}
	return NULL;
}

